package io.renren.modules.sys.controller;

import io.renren.common.utils.R;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("sys/upload/")
public class ImgController {

	@ResponseBody
	@RequestMapping("imgUpload")
	public R imgUpload(@RequestParam(value = "imgFile", required = false) MultipartFile imgFile, MultipartHttpServletRequest request,
			HttpServletResponse response) throws IOException {
		if (imgFile.getSize() != 0) {
			String fileName = imgFile.getOriginalFilename();
			String reg = ".+(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png|.mp4)$";
			Pattern pattern = Pattern.compile(reg);
			Matcher matcher = pattern.matcher(fileName.toLowerCase());
			if (!matcher.find()) {
				return R.error().put("error",-1).put("message","图片格式不正确，请上传 jpeg,jpg,gif,bmp,png格式图片").put("msg","图片格式不正确，请上传 jpeg,jpg,gif,bmp,png格式图片");
			}

			if (imgFile.getSize() > 1024 * 1024 * 10) {
				return R.error().put("error",-1).put("message","图片最大10M！").put("msg","图片最大10M！");
			}

			String uuidname = UUID.randomUUID().toString();
			String upload = ResourceUtils.getURL("classpath:").getPath()+"/statics/upload";
			String webPath = "/statics/upload";
			// 时间文件夹
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			upload += "/" + sdf.format(new Date())+"/";
			webPath += "/" + sdf.format(new Date())+"/";
			String fileType = fileName.substring(fileName.lastIndexOf("."));
			File uploadFile = new File(upload);
			if (!uploadFile.exists()) {
				uploadFile.mkdirs();
			}
			File file = new File(upload+uuidname+fileType);
			imgFile.transferTo(file);
			/*if (!imgFile.isEmpty()) {
				try {
					byte[] bytes = imgFile.getBytes();
					BufferedOutputStream stream =
							new BufferedOutputStream(new FileOutputStream(new File(upload+uuidname+fileType)));
					stream.write(bytes);
					stream.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}*/
			/*;*/
			/*List<MultipartFile> files = request.getFiles("imgFile");

			Iterator<MultipartFile> itr = files.iterator();
			while (itr.hasNext()) {
				MultipartFile item = itr.next();

				try {
					File uploadedFile = new File(upload, uuidname + fileType);
					item.transferTo(uploadedFile);

				} catch (Exception e) {
					//writer.println(objectMapper.writeValueAsString(getError("上传文件失败。")));
					e.printStackTrace();
				}


			}*/
			return R.ok().put("error",0).put("url",webPath+uuidname+fileType);
			/*upload += "/" + uuidname + fileType;
			webPath += "/" + uuidname + fileType;
			InputStream in = imgFile.getInputStream();
			OutputStream out = new FileOutputStream(upload);
			IOUtils.In2Out(in, out);*/
			//IOUtils.close(in, out);


		}

		return R.error("请不要上传空文件！");
	}

	@RequestMapping("imgDownload")
	public void downloadImg(String path, HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher(path).forward(request, response);
	}

}
